#!/bin/sh
LIST_APP=$(/usr/local/glassfish4/glassfish/bin/asadmin list-applications -t)
echo "deployed: $LIST_APP"
OLD_WAR=$(echo $LIST_APP | awk '{print $1}')
NEW_WAR=/tmp/dataverse.war
/usr/local/glassfish4/glassfish/bin/asadmin undeploy $OLD_WAR
/usr/local/glassfish4/glassfish/bin/asadmin stop-domain
# blow away "generated" directory to avoid EJB Timer Service is not available" https://github.com/IQSS/dataverse/issues/3336
rm -rf /usr/local/glassfish4/glassfish/domains/domain1/generated
rm -rf /usr/local/glassfish4/glassfish/domains/domain1/files
#psql -U postgres -c "CREATE ROLE dvnapp UNENCRYPTED PASSWORD 'secret' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN" template1
psql -U dvnapp -c 'DROP DATABASE "dvndb"' template1
echo $?
curl http://localhost:8983/solr/collection1/update/json?commit=true -H "Content-type: application/json" -X POST -d "{\"delete\": { \"query\":\"*:*\"}}"
psql -U dvnapp -c 'CREATE DATABASE "dvndb" WITH OWNER = "dvnapp"' template1
echo $?
/usr/local/glassfish4/glassfish/bin/asadmin start-domain
/usr/local/glassfish4/glassfish/bin/asadmin deploy $NEW_WAR
